/*
编写 : Hexh
时间 : 2012-6-13 18:28:00
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Newtonsoft.Json;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of FsCartransferDetail
	/// </summary>
	public class FsCartransferDetailController : BaseController
	{

		private readonly IFsCartransferDetailFacade FsCartransferDetailFacade;
		private readonly IFsCartransferFacade FsCartransferFacade;
		private readonly IFsCarbaseinfoFacade FsCarbaseinfoFacade;
		private readonly IHospUserFacade HospUserFacade;

		public FsCartransferDetailController(IFsCartransferDetailFacade FsCartransferDetailFacade, IHospUserFacade HospUserFacade,
			IFsCarbaseinfoFacade FsCarbaseinfoFacade, IFsCartransferFacade FsCartransferFacade)
		{
			this.FsCartransferDetailFacade = FsCartransferDetailFacade;
			this.HospUserFacade = HospUserFacade;
			this.FsCarbaseinfoFacade = FsCarbaseinfoFacade;
			this.FsCartransferFacade = FsCartransferFacade;
		}
		
		[Description("访问FsCartransferDetail管理页面")]
		[ViewPage]
		public ActionResult FsCartransferDetail()
		{
			return View();
		}
		
		[ExtResult]
		[Description("添加修改FsCartransferDetail信息")]
		public ActionResult SaveOrUpdate()
		{
			FsCartransferDetail entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new FsCartransferDetail
				{
					TransferId = Request["TransferId"], 
					CarId = Request["CarId"], 
					ExecUser = Request["ExecUser"], 
					State = Request["State"], 
				};
			}
			else
			{
				// get the entity.
				entity = FsCartransferDetailFacade.Get(Request["Id"]);
				// set new value..
				entity.TransferId = Request["TransferId"]; 
				entity.CarId = Request["CarId"]; 
				entity.ExecUser = Request["ExecUser"]; 
				entity.State = Request["State"]; 
			}
			// 保存信息
			FsCartransferDetailFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[ExtResult]
		[Description("修改商品车调拨单的车辆信息")]
		public ActionResult SaveEditRecord()
		{
			var tfs = JsonConvert.DeserializeObject<List<FsCartransferDetail>>(Request["datas"]);
			foreach(FsCartransferDetail tf in tfs)
			{
				// todo 排除多次更新的项
				// Id 是protect 只能用Code 代替了..
				FsCartransferDetail entity = FsCartransferDetailFacade.Get(tf.Code);
				entity.ExecUser = tf.ExecUser;
				entity.State = tf.State;
				FsCartransferDetailFacade.SaveOrUpdate(entity);
			}
			return this.JsonFormat(new ExtResult { success = true });
		}

		[ExtResult]
		[Description("添加修改FsCartransferDetail信息")]
		public ActionResult AddByCarInfo()
		{
			FsCarbaseinfo car = FsCarbaseinfoFacade.GetByQuery(Request["car_query"]);
			if (car == null)
				throw new ValidationException("未找到商品车信息.");

			FsCartransfer transfer = FsCartransferFacade.Get(Request["TransferId"]);
			//判断
			if (transfer.State != FsCartransferState.Editing || transfer.OutDeptId != car.DepartmentId || car.State != FsCarState.InStore)
				throw new ValidationException("调拨单与车辆状态校验错。");

			//判断重复
			if (FsCartransferDetailFacade.IsFieldExistWhere("CarId", car.Id, "", " TransferId = '" + transfer.Id + "'"))
				throw new ValidationException("调拨单已存在该车辆。");

			FsCartransferDetail entity = new FsCartransferDetail
			{	
				CarId = car.Id,
				TransferId = Request["TransferId"],
				State = "待提出",
				ExecUser = Request["ExecUser"]
			};
			FsCartransferDetailFacade.SaveOrUpdate(entity);
			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}


		[ExtResult]
		[Description("删除FsCartransferDetail基本资料")]
		public ActionResult Delete()
		{
			FsCartransferDetailFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
		
		[ExtResult]
		[Description("物理删除FsCartransferDetail基本资料")]
		public ActionResult PhysicsDelete()
		{
			FsCartransferDetailFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取FsCartransferDetail清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = FsCartransferDetailFacade.GetPlist(
				Request["transferId"],
				Request["q_query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			//TODO : 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<FsCartransferDetail> { total = total, data = data },
				new string[] { "Car" });
		}
		
		[Description("获取FsCartransferDetail信息")]
		public ActionResult GetById()
		{
			var entity = FsCartransferDetailFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<FsCartransferDetail> { data = new FsCartransferDetail[] { entity } }, new string[] { "AuthorizeUsers" }
			   );
		}

		[ExtResult]
		[Description("判断FsCartransferDetail字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = FsCartransferDetailFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
